{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [10 11 12 13 14 15]\n",
      " [20 21 22 23 24 25]\n",
      " [30 31 32 33 34 35]\n",
      " [40 41 42 43 44 45]\n",
      " [50 51 52 53 54 55]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.arange(0,60,10).reshape(-1,1) + np.arange(0,6)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4]\n",
      "[[44 45]\n",
      " [54 55]]\n",
      "[ 2 12 22 32 42 52]\n",
      "[[20 22 24]\n",
      " [40 42 44]]\n"
     ]
    }
   ],
   "source": [
    "print(a[0,3:5])\n",
    "print(a[4:,4:])\n",
    "print(a[:,2])\n",
    "print((a[2::2,::2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-3  4]\n"
     ]
    }
   ],
   "source": [
    "b = a[0,3:5]\n",
    "b[0] = -b[0]\n",
    "print(a[0, 3:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2 -3  4  5]\n",
      " [22 23 24 25]\n",
      " [42 43 44 45]]\n",
      "[[ 4  5]\n",
      " [44 45]]\n"
     ]
    }
   ],
   "source": [
    "idx = slice(None,None, 2), slice(2, None)\n",
    "print(a[idx])\n",
    "print(a[idx][idx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(slice(None, None, 2), slice(2, None, None))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.s_[::2,2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<numpy.lib.index_tricks.IndexExpression at 0x7f7daf8812d0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.s_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(slice(None, None, 2), slice(2, None, None))\n"
     ]
    }
   ],
   "source": [
    "class S(object):\n",
    "    def __getitem__(self, index):\n",
    "        return index\n",
    "\n",
    "print(S()[::2,2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1 12 23 34]\n",
      "[[30 32 35]\n",
      " [40 42 45]\n",
      " [50 52 55]]\n",
      "[ 2 22 52]\n"
     ]
    }
   ],
   "source": [
    "print(a[(0,1,2,3),(1,2,3,4)])\n",
    "print(a[3:, [0,2,5]])\n",
    "mask = np.array([1,0,1,0,0,1], dtype=np.bool)\n",
    "print(a[mask,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12  2 12  2  2 12]\n",
      "[ 2 22 52]\n"
     ]
    }
   ],
   "source": [
    "mask = np.array([1,0,1,0,0,1])\n",
    "print(a[mask, 2])\n",
    "mask = [True, False, True, False, False, True]\n",
    "print(a[mask, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 11 12 13 14 15]\n",
      " [20 21 22 23 24 25]]\n"
     ]
    }
   ],
   "source": [
    "print(a[[1,2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 14]\n",
      " [23 32]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[0,1],[2,3]])\n",
    "y = np.array([[-1,-2],[-3,-4]])\n",
    "print(a[x,y])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 14]\n",
      " [23 32]]\n"
     ]
    }
   ],
   "source": [
    "print(a[(0,1,2,3),(-1,-2,-3,-4)].reshape(2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2 -3  4  5]\n",
      "  [10 11 12 13 14 15]]\n",
      "\n",
      " [[20 21 22 23 24 25]\n",
      "  [30 31 32 33 34 35]]]\n"
     ]
    }
   ],
   "source": [
    "print(a[x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[  0   0   0]\n",
      "  [255   0   0]\n",
      "  [  0 255   0]\n",
      "  [  0   0   0]]\n",
      "\n",
      " [[  0   0   0]\n",
      "  [  0   0 255]\n",
      "  [255 255 255]\n",
      "  [  0   0   0]]]\n"
     ]
    }
   ],
   "source": [
    "palette = np.array( [ [0,0,0],\n",
    "                  [255,0,0],\n",
    "                  [0,255,0],\n",
    "                  [0,0,255],\n",
    "                  [255,255,255] ])\n",
    "image = np.array([[0,1,2,0],\n",
    "                 [0,3,4,0]])\n",
    "print(palette[image])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
